package com.parenting.oauth2.service;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;

@Data
public class JwtUser implements UserDetails {

    @ApiModelProperty(value = "用户ID")
    private String uid;

    @ApiModelProperty(value = "用户名")
    private String username;
    @ApiModelProperty(value = "账号")
    private String account;
  //  @JSONField(serialize = false) // 忽略转json
    @JsonIgnore
    @ApiModelProperty(value = "密码，加密存储, admin/1234")
    private String password;

    @ApiModelProperty(value = "昵称")
    private String nickName;

    @ApiModelProperty(value = "头像url")
    private String imageUrl;

    @ApiModelProperty(value = "注册手机号")
    private String mobile;

    @ApiModelProperty(value = "注册邮箱")
    private String email;

    @ApiModelProperty(value = "第三方的 sessionKey")
    private String sessionKey;

    @ApiModelProperty(value = "第三方的 openId唯一标识")
    private String openId;

    // 1 true 0 false
  //  @JSONField(serialize = false) // 忽略转json
    @JsonIgnore
    @ApiModelProperty(value = "帐户是否过期(1 未过期，0已过期)")
    private boolean isAccountNonExpired; // 不要写小写 boolean

  //  @JSONField(serialize = false) // 忽略转json
    @JsonIgnore
    @ApiModelProperty(value = "帐户是否被锁定(1 未过期，0已过期)")
    private boolean isAccountNonLocked;

   // @JSONField(serialize = false) // 忽略转json
    @JsonIgnore
    @ApiModelProperty(value = "密码是否过期(1 未过期，0已过期)")
    private boolean isCredentialsNonExpired;

   // @JSONField(serialize = false) // 忽略转json
    @JsonIgnore
    @ApiModelProperty(value = "帐户是否可用(1 可用，0 删除用户)")
    private boolean isEnabled;

    @ApiModelProperty(value = "店铺ID")
    private  String shopId;

    @ApiModelProperty(value = "店铺名称")
    private  String shopName;

    @ApiModelProperty(value = "店铺状态 1.未认领，2.闭店、3待审批、4已审批、5已拒绝")
    private  Integer shopState;
    /**
     * 封装用户拥有的菜单权限标识
     */
   // @JSONField(serialize = false) // 忽略转json
    @JsonIgnore
    private List<GrantedAuthority> authorities;


    //    isAccountNonExpired 是 Integer 类型接收，然后转 boolean
    public JwtUser(String uid, String username, String account, String password,
                   String nickName, String imageUrl, String mobile, String email,
                   Integer isAccountNonExpired, Integer isAccountNonLocked,
                   Integer isCredentialsNonExpired, Integer isEnabled, String sessionKey, String openId, String shopId,String shopName,
                   List<GrantedAuthority> authorities,Integer shopState) {
        this.uid = uid;
        this.username = username;
        this.account = account;
        this.password = password;
        this.nickName = nickName;
        this.imageUrl = imageUrl;
        this.mobile = mobile;
        this.email = email;
        this.isAccountNonExpired = isAccountNonExpired == 1 ? true : false;
        this.isAccountNonLocked = isAccountNonLocked == 1 ? true : false;
        this.isCredentialsNonExpired = isCredentialsNonExpired == 1 ? true : false;
        this.isEnabled = isEnabled == 1 ? true : false;
        this.sessionKey = sessionKey;
        this.openId = openId;
        this.authorities = authorities;
        this.shopId = shopId;
        this.shopName=shopName;
        this.shopState=shopState;
    }

    public JwtUser(Long id, String nickname, Integer accountNum, String imgUrl, String mobile, List<GrantedAuthority> authorities) {
        this.uid = String.valueOf(id);
        this.username = nickname;
        this.account = String.valueOf(accountNum);
        this.imageUrl = imgUrl;
        this.mobile = mobile;
        this.authorities = authorities;
    }
}